package com.qianguo.buyer.tab;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.eva.androidlibrary.api.HttpRequestCallback;
import com.eva.androidlibrary.api.entity.OrderInfo;
import com.eva.androidlibrary.api.entity.UserInfo;
import com.eva.androidlibrary.widget.SwipeRefreshRecyclerView;
import com.qianguo.buyer.BuyerLoginActivity;
import com.qianguo.buyer.R;
import com.qianguo.buyer.base.BaseFragment;
import com.qianguo.buyer.entity.BuyerOrder;
import com.qianguo.buyer.entity.BuyerOrderList;
import com.qianguo.buyer.order.CancelOrderFragment_;
import com.qianguo.buyer.order.DoingOrderFragment_;
import com.qianguo.buyer.order.DoneOrderFragment_;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * TAB 买家订单
 * @author eva
 * @date 16/6/28
 */
@EFragment(R.layout.fragment_tab_buyer_order)
public class BuyerOrderFragment extends BaseFragment {


    @ViewById(R.id.order_recycler_view)
    SwipeRefreshRecyclerView mOrderRecyclerView;

    @ViewById(R.id.buyer_order_login_button)
    Button mBtnLogin;


    @Override
    public void onDetach() {
        super.onDetach();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initializeUI() {
        EventBus.getDefault().register(this);
        if (mApp.isLogin()) {
            initViewPager();
        } else {
            mOrderRecyclerView.setVisibility(View.GONE);
        }
    }

    private void initViewPager() {
        mBtnLogin.setVisibility(View.GONE);
    }

    @Click(R.id.buyer_order_login_button)
    void clickLogin() {
        BuyerLoginActivity.jumpToLogin(getActivity());
    }

    @Background
    void getOrders(int page, int perPage) {
        mApp.getApi().getOrders(mApp.getUser().getToken(), BuyerOrder.TYPE_CANCEL, page, perPage, new HttpRequestCallback<BuyerOrderList>() {
            @Override
            public void onSuccess(int code, String msg, BuyerOrderList data) {
            }

            @Override
            public void onFailure(int code, String msg) {
                showSnackBar(getActivity(), msg);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserInfo user) {
        initViewPager();
    }

    private class OrderAdapter extends RecyclerView.Adapter<OrderViewHolder> {

        @Override
        public OrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return null;
        }

        @Override
        public void onBindViewHolder(OrderViewHolder holder, int position) {

        }

        @Override
        public int getItemCount() {
            return 0;
        }
    }

    private class OrderViewHolder extends RecyclerView.ViewHolder {

        TextView tvTime;
        GridView products;
        GridView status;
        TextView tvPrice;
        Button btnAction;
        public OrderViewHolder(View itemView) {
            super(itemView);
            tvTime = (TextView) itemView.findViewById(R.id.item_order_time_text_view);
            products = (GridView) itemView.findViewById(R.id.item_order_products_grid_view);
            status = (GridView) itemView.findViewById(R.id.item_order_status_grid_view);
            tvPrice = (TextView) itemView.findViewById(R.id.item_order_price_text_view);
            btnAction = (Button) itemView.findViewById(R.id.item_order_action_button);
        }

        private void onBind(OrderInfo order) {

        }
    }


}
